package com.runfast.waimai.web.controller.Runfastbusinesscontroller;


import com.runfast.waimai.dao.model.RunfastDistributionassess;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.dao.model.RunfastShoppererror;
import com.runfast.waimai.service.RunfastDistributionassessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
/* 统计团队下骑手的差评问题
*autor mengxiang
* time 2018/6/4
* */
@Controller
@RequestMapping("/shopper/*")
public class ShopperErrController {
@Resource
    RunfastGoodsSellRecordService runfastGoodsSellRecordService;
@Resource
    RunfastDistributionassessService runfastDistributionassessService;

    @PostMapping("error")// 统计团队下骑手的差评问题
    public @ResponseBody ModelMap error(String cityname,String shoppername, String createatime1,String createatime2,Integer errend,Integer a,
                                        @RequestParam(value="page",required=false,defaultValue = "0") Integer page,
                                        @RequestParam(value="pagesize",required=false,defaultValue = "10") Integer pagesize
                                        ){
        //cityname为城市名，shoppername为骑手名， createatime1为开始时间， createatime2 为结束时间 ，Interger errend为已完成取消定单 1 可以不写，
        //a=1 最近一天，a=2 最近七天， a=3 最近一个月
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        ModelMap model=new ModelMap();
        RunfastShoppererror shoppererror=new RunfastShoppererror();
        // 要显示 代理商 配送员名称 有效完成单 配送所得金额   超时单，超时率，异常取消单， 异常取消率 差评数， 差评率



        try {
            Date date1=format.parse(createatime1);
            Date date2=format.parse(createatime2);
            Date date3=new Date();//date3为系统当前时间
            Calendar Cal=Calendar.getInstance();
            Cal.setTime(date3);
            if(a==1){
                Cal.add(java.util.Calendar.HOUR_OF_DAY,-24);
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,Cal.getTime(),date3,page,pagesize);
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                }else {

                    model.put("listmap",listmap);
                    model.put("begin",Cal.getTime());
                    model.put("over",date3);

                }
            }else  if(a==2){
                Cal.add(java.util.Calendar.HOUR_OF_DAY,-24*7);
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,Cal.getTime(),date3,page,pagesize);
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                }else {

                    model.put("listmap",listmap);
                    model.put("begin",Cal.getTime());
                    model.put("over",date3);
                }
            }else if(a==3){
                Cal.add(java.util.Calendar.HOUR_OF_DAY,-24*30);
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,Cal.getTime(),date3,page,pagesize);
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                }else {

                    model.put("listmap",listmap);
                    model.put("begin",Cal.getTime());
                    model.put("over",date3);
                }
            } else {
                List<Map<String,BigDecimal>>listmap= runfastGoodsSellRecordService.findshopperAndCityAndtime(cityname,shoppername,date1,date2,page,pagesize);
                if(null==listmap){
                    model.put("msg","亲，没有你要的数据");
                }else {

                    model.put("listmap",listmap);
                    model.put("begin",date1);
                    model.put("over",date2);

                }
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  model;
    }
/*
* 超时单查询
* */
    @PostMapping("aftertime")
    public @ResponseBody ModelMap aftertime(String createatime1,String createatime2,String cityname,String shoppername){
        ModelMap model=new ModelMap();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1=dateFormat.parse(createatime1);
            Date date2=dateFormat.parse(createatime2);
           List<RunfastGoodsSellRecord>list= runfastGoodsSellRecordService.findaftertime(date1,date2,cityname,shoppername);
          if(list==null){ model.put("msg","没有超时的定单，你太棒了");
          }else {model.put("list",list);}
        } catch (ParseException e) {
            e.printStackTrace();
        }
             return  model;
    }
/*
* 异常取消单
* */
@PostMapping("errorOver")
public @ResponseBody ModelMap errorOver(String createatime1,String createatime2,String cityname,String shoppername){
    ModelMap model=new ModelMap();
    DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date date1=dateFormat.parse(createatime1);
        Date date2=dateFormat.parse(createatime2);
        List<RunfastGoodsSellRecord>list= runfastGoodsSellRecordService.finderrorOver(date1,date2,cityname,shoppername);
        if(list==null){ model.put("msg","没有异常取消单");
        }else {model.put("list",list);}
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return  model;
}
    /*
     * 差评单
     * */
    @PostMapping("lastscore")
    public @ResponseBody ModelMap lastscore(String createatime1,String createatime2,String cityname,String shoppername){
        ModelMap model=new ModelMap();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1=dateFormat.parse(createatime1);
            Date date2=dateFormat.parse(createatime2);
            List<RunfastGoodsSellRecord>list= runfastGoodsSellRecordService.findlastscore(date1,date2,cityname,shoppername);
            if(list==null){ model.put("msg","没有差评单");
            }else {model.put("list",list);}
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  model;
    }

}
